﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.Serialization;
using System.IO;

namespace Artix
{
    [DataContract]
    public class BubbleSeries
    {
        private object[][] _values;
        [DataMember(Name = "values", Order = 1, EmitDefaultValue = true)]
        public object[][] Values
        { get { return _values; } set { _values = value; } }

        private string _label;
        [DataMember(Name = "text", Order = 2, EmitDefaultValue = false)]
        public string Label
        { get { return _label; } set { _label = value; } }

        private string _backgroundColor;
        [DataMember(Name = "background-color", Order = 3, EmitDefaultValue = false)]
        public string BackgroundColor
        { get { return _backgroundColor; } set { _backgroundColor = value; } }

        private string _backgroundColor2;
        [DataMember(Name = "background-color-2", Order = 4, EmitDefaultValue = false)]
        public string BackgroundColor2
        { get { return _backgroundColor2; } set { _backgroundColor2 = value; } }

        private Marker _marker;
        [DataMember(Name = "marker", Order = 6, EmitDefaultValue = false)]
        public Marker Marker
        { get { return _marker; } set { _marker = value; } }

    }

    [DataContract]
    public class BubbleChart
    {
        private XScale _xScale;
        [DataMember(Name = "scale-x", EmitDefaultValue = false)]
        public XScale XScale
        { get { return _xScale; } set { _xScale = value; } }

        private List<BubbleSeries> _series = new List<BubbleSeries>();
        [DataMember(Name = "series", Order = 2)]
        public List<BubbleSeries> Series
        { get { return _series; } set { _series = value; } }

        /// <summary>
        /// For bubble charts
        /// </summary>
        /// <param name="values"></param>
        public void AddArray(object[] values)
        {
            /*StringBuilder list = new StringBuilder();
            foreach (decimal d in values)
            {
                if (list.Length > 0)
                    list.Append(",");

                list.Append(d.ToString());
            }*/
            object[][] jaggedArray = new object[values.Count()][];
            for (int i = 0; i < values.Count(); i++)
            {
                jaggedArray[i] = new object[3];
                jaggedArray[i][0] = values[0];
            }

            int series = _series.Count - 1;

            _series[series].Values = jaggedArray;
        }

        public void AddJaggedArray(object[][] values)
        {
            /*StringBuilder list = new StringBuilder();
            foreach (decimal d in values)
            {
                if (list.Length > 0)
                    list.Append(",");

                list.Append(d.ToString());
            }*/
            _series.Add(new BubbleSeries());
            int series = _series.Count - 1;

            _series[series].Values = values;
        }

        public string ValuesToJSON()
        {
            System.Runtime.Serialization.Json.DataContractJsonSerializer serializer = new System.Runtime.Serialization.Json.DataContractJsonSerializer(this.Series.GetType());
            MemoryStream ms = new MemoryStream();
            serializer.WriteObject(ms, this.Series);
            string json = Encoding.Default.GetString(ms.ToArray());
            return json;
        }

        public string ToJSON()
        {
            System.Runtime.Serialization.Json.DataContractJsonSerializer serializer = new System.Runtime.Serialization.Json.DataContractJsonSerializer(this.GetType());
            MemoryStream ms = new MemoryStream();
            serializer.WriteObject(ms, this);
            string json = Encoding.Default.GetString(ms.ToArray());
            return json;
        }
    }
}
